<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <meta name="description" content="title: seleniumdate: 2021-03-03 09:49:25tags:
selenium模拟注册，登陆以及图片验证码的生成1.模拟注册
1234567891011121314151617181920#selenum 模拟注册from selenium import webdriv" />
  

  
  
  
  
  
  
  <title>南辞的技术博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="title: seleniumdate: 2021-03-03 09:49:25tags: selenium模拟注册，登陆以及图片验证码的生成1.模拟注册 1234567891011121314151617181920#selenum 模拟注册from selenium import webdriverfrom selenium.webdriver import ActionChains#生成浏览">
<meta property="og:type" content="article">
<meta property="og:title" content="南辞的技术博客">
<meta property="og:url" content="https://yanlidoushikeke.gitee.io/myhexo/2021/01/19/selenium%E6%A8%A1%E6%8B%9F%E6%B3%A8%E5%86%8C%EF%BC%8C%E7%99%BB%E9%99%86%E4%BB%A5%E5%8F%8A%E5%9B%BE%E7%89%87%E9%AA%8C%E8%AF%81%E7%A0%81%E7%9A%84%E7%94%9F%E6%88%90/index.html">
<meta property="og:site_name" content="南辞的技术博客">
<meta property="og:description" content="title: seleniumdate: 2021-03-03 09:49:25tags: selenium模拟注册，登陆以及图片验证码的生成1.模拟注册 1234567891011121314151617181920#selenum 模拟注册from selenium import webdriverfrom selenium.webdriver import ActionChains#生成浏览">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-19T12:22:49.270Z">
<meta property="article:modified_time" content="2021-03-03T03:44:09.408Z">
<meta property="article:author" content="南辞">
<meta name="twitter:card" content="summary">
  
  
    <link rel="icon" href="/tefuir/css/images/favicon.ico">
  
  
<link rel="stylesheet" href="/tefuir/css/style.css">

  

  
  <!-- baidu webmaster push -->
  <script src='//push.zhanzhang.baidu.com/push.js'></script>
<meta name="generator" content="Hexo 5.2.0"><link rel="alternate" href="/tefuir/atom.xml" title="南辞的技术博客" type="application/atom+xml">
</head>
<body class="home blog custom-background custom-font-enabled single-author">
  <div id="page" class="hfeed site">
      <header id="masthead" class="site-header" role="banner">
    <hgroup>
      <h1 class="site-title">
        <a href="/tefuir/" title="南辞的技术博客" rel="home">南辞的技术博客</a>
      </h1>
      
        <h2 class="site-description hitokoto"></h2>
        <script type="text/javascript" src="https://v1.hitokoto.cn/?encode=js"></script>
      
    </hgroup>

    <nav id="site-navigation" class="main-navigation" role="navigation">
            <button class="menu-toggle">菜单</button>
            <a class="assistive-text" href="/#content" title="跳至内容">跳至内容</a><!--TODO-->
            <div class="menu-main-container">
                <ul id="menu-main" class="nav-menu">
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/tefuir/">Home</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/tefuir/archives">Archives</a></li>
                
                </ul>
            </div>
    </nav>
</header>

      <div id="main" class="wrapper">
        <div id="primary" class="site-content"><div id="content" role="main"><article id="post-selenium模拟注册，登陆以及图片验证码的生成" class="post-selenium模拟注册，登陆以及图片验证码的生成 post type-post status-publish format-standard hentry">
    <!---->

      <header class="entry-header">
        
        <div class="comments-link">
            
            <a href="javascript:void(0);" data-url="https://yanlidoushikeke.gitee.io/myhexo/2021/01/19/selenium%E6%A8%A1%E6%8B%9F%E6%B3%A8%E5%86%8C%EF%BC%8C%E7%99%BB%E9%99%86%E4%BB%A5%E5%8F%8A%E5%9B%BE%E7%89%87%E9%AA%8C%E8%AF%81%E7%A0%81%E7%9A%84%E7%94%9F%E6%88%90/" data-id="ckkttwp6v0005csu0b1z29u45" class="leave-reply bdsharebuttonbox" data-cmd="more">Share</a>
        </div><!-- .comments-link -->
      </header><!-- .entry-header -->

    <div class="entry-content">
      
        <p>title: selenium<br>date: 2021-03-03 09:49:25<br>tags:</p>
<h1 id="selenium模拟注册，登陆以及图片验证码的生成"><a href="#selenium模拟注册，登陆以及图片验证码的生成" class="headerlink" title="selenium模拟注册，登陆以及图片验证码的生成"></a>selenium模拟注册，登陆以及图片验证码的生成</h1><p>1.模拟注册</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#selenum 模拟注册</span><br><span class="line">from selenium import webdriver</span><br><span class="line"></span><br><span class="line">from selenium.webdriver import ActionChains</span><br><span class="line"></span><br><span class="line">#生成浏览器对象 chromedriver.exe的路径</span><br><span class="line">browser &#x3D; webdriver.Chrome(&#39;C:&#x2F;Users\逸可\AppData\Local\Google\Chrome\Application\chromedriver_win32\chromedriver.exe&#39;)</span><br><span class="line"></span><br><span class="line">#打开页面</span><br><span class="line">browser.get(&#39;http:&#x2F;&#x2F;127.0.0.1:8080&#x2F;head&#x2F;&#39;)</span><br><span class="line"></span><br><span class="line">#元素选择</span><br><span class="line">browser.find_elements_by_tag_name(&quot;input&quot;)[2].send_keys(&#39;南辞&#39;)</span><br><span class="line">browser.find_elements_by_tag_name(&quot;input&quot;)[3].send_keys(&#39;123&#39;)</span><br><span class="line">browser.find_elements_by_tag_name(&quot;input&quot;)[4].send_keys(&#39;123&#39;)</span><br><span class="line">browser.find_elements_by_tag_name(&quot;input&quot;)[5].send_keys(&#39;1234876543&#39;)</span><br><span class="line">browser.find_elements_by_tag_name(&quot;input&quot;)[6].send_keys(&#39;12345@qq.com&#39;)</span><br><span class="line"></span><br><span class="line">#点击登录</span><br><span class="line">browser.find_element_by_class_name(&#39;van-button--primary&#39;).click()</span><br></pre></td></tr></table></figure>

<p>2.模拟登陆</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">#selenum 模拟登陆</span><br><span class="line">from selenium import webdriver</span><br><span class="line"></span><br><span class="line">from selenium.webdriver import ActionChains</span><br><span class="line"></span><br><span class="line">#生成浏览器对象</span><br><span class="line">browser &#x3D; webdriver.Chrome(&#39;C:&#x2F;Users\逸可\AppData\Local\Google\Chrome\Application\chromedriver_win32\chromedriver.exe&#39;)</span><br><span class="line"></span><br><span class="line">#打开页面</span><br><span class="line">browser.get(&#39;http:&#x2F;&#x2F;127.0.0.1:8080&#x2F;denglu&#x2F;&#39;)</span><br><span class="line"></span><br><span class="line">#元素选择</span><br><span class="line">browser.find_elements_by_tag_name(&quot;input&quot;)[0].send_keys(&#39;111&#39;)</span><br><span class="line">browser.find_elements_by_tag_name(&quot;input&quot;)[1].send_keys(&#39;123&#39;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#拖拽动作</span><br><span class="line">action &#x3D; ActionChains(browser)</span><br><span class="line">#获取拖拽动作的标签</span><br><span class="line">button &#x3D; browser.find_element_by_class_name(&quot;dv_handler&quot;)</span><br><span class="line"></span><br><span class="line">#按住不松，拖动</span><br><span class="line">action.click_and_hold(button).perform()</span><br><span class="line">action.reset_actions()</span><br><span class="line">action.move_by_offset(280,0).perform()</span><br><span class="line"></span><br><span class="line">#点击登录</span><br><span class="line">browser.find_element_by_class_name(&#39;van-button--primary&#39;).click()</span><br></pre></td></tr></table></figure>

<p>3.图片验证码</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Mycode</span>(<span class="params">APIView</span>):</span></span><br><span class="line">   <span class="function"><span class="keyword">def</span> <span class="title">get_random_color</span>(<span class="params">self</span>):</span></span><br><span class="line">      R = random.randrange(<span class="number">255</span>)</span><br><span class="line">      G = random.randrange(<span class="number">255</span>)</span><br><span class="line">      B = random.randrange(<span class="number">255</span>)</span><br><span class="line">      <span class="keyword">return</span> (R,G,B)</span><br><span class="line">   <span class="function"><span class="keyword">def</span> <span class="title">get</span>(<span class="params">self,request</span>):</span></span><br><span class="line">      <span class="comment">#定义画布</span></span><br><span class="line">      img_size = (<span class="number">120</span>,<span class="number">50</span>)</span><br><span class="line"></span><br><span class="line">      <span class="comment">#定义图像对象</span></span><br><span class="line">      image = Image.new(<span class="string">&#x27;RGB&#x27;</span>,img_size,<span class="string">&#x27;white&#x27;</span>)</span><br><span class="line">      <span class="comment">#定义画笔</span></span><br><span class="line">      draw = ImageDraw.Draw(image,<span class="string">&#x27;RGB&#x27;</span>)</span><br><span class="line">      <span class="comment">#定义字符串</span></span><br><span class="line">      source = <span class="string">&#x27;qwertiop234gh&#x27;</span></span><br><span class="line">      <span class="comment">#定义容器</span></span><br><span class="line">      code_str = <span class="string">&#x27;&#x27;</span></span><br><span class="line">      <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">4</span>):</span><br><span class="line">         <span class="comment">#获取字符串颜色</span></span><br><span class="line">         text_color = self.get_random_color()</span><br><span class="line">         <span class="comment">#获取随机字符</span></span><br><span class="line">         tem_num = random.randrange(<span class="built_in">len</span>(source))</span><br><span class="line">         random_str = source[tem_num]</span><br><span class="line">         code_str += random_str</span><br><span class="line">         draw.text((<span class="number">10</span>+<span class="number">30</span>*i,<span class="number">20</span>),random_str,text_color)</span><br><span class="line">      buf = io.BytesIO()</span><br><span class="line">      image.save(buf,<span class="string">&#x27;png&#x27;</span>)</span><br><span class="line">      <span class="comment">#保存验证码</span></span><br><span class="line">      request.session[<span class="string">&#x27;code&#x27;</span>] = code_str</span><br><span class="line">      <span class="keyword">return</span> HttpResponse(buf.getvalue(),<span class="string">&#x27;image/pag&#x27;</span>)</span><br></pre></td></tr></table></figure>

<p>图片验证码的前端</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">&lt;div&gt;</span><br><span class="line">    &lt;van-cell-group&gt;</span><br><span class="line"></span><br><span class="line">      &lt; img: src &#x3D; &#39;src&#39; @ click &#x3D; &quot;changecode&quot; &gt;</span><br><span class="line"></span><br><span class="line">   &lt;&#x2F;van-cell-group&gt;</span><br><span class="line"></span><br><span class="line">   &lt;van-field v-model&#x3D;&quot;code&quot; lable &#x3D; &#39;验证码&#39;placeholder &#x3D; &quot;请输入验证码&quot; class&#x3D;&quot;img&quot;&gt;</span><br><span class="line"></span><br><span class="line">   &lt;&#x2F;van-field&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;div&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">methods: &#123;</span><br><span class="line">        &#x2F;&#x2F;变更验证码</span><br><span class="line">        changecode()&#123;</span><br><span class="line">                var num &#x3D; Math.ceil(Math.random()*10);</span><br><span class="line">                this.src &#x3D; this.src + &#39;?num&#39; + num;</span><br><span class="line">        &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">img&#123;</span><br><span class="line">	#鼠标无需点击就能更换验证码</span><br><span class="line">    cursor: pointer;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;style&gt;</span><br></pre></td></tr></table></figure>



<p>​      </p>

      
    </div><!-- .entry-content -->

    <footer class="entry-meta">
    <a href="/tefuir/2021/01/19/selenium%E6%A8%A1%E6%8B%9F%E6%B3%A8%E5%86%8C%EF%BC%8C%E7%99%BB%E9%99%86%E4%BB%A5%E5%8F%8A%E5%9B%BE%E7%89%87%E9%AA%8C%E8%AF%81%E7%A0%81%E7%9A%84%E7%94%9F%E6%88%90/">
    <time datetime="2021-01-19T12:22:49.270Z" class="entry-date">
        2021-01-19
    </time>
</a>
    
    
    </footer>
</article>


    
<nav class="nav-single">
    <h3 class="assistive-text">文章导航</h3>
    
        <span class="nav-previous"><a href="/tefuir/2021/01/21/%E7%AE%80%E8%BF%B0vue%E5%92%8Cdjango%E7%9A%84%E5%B7%A5%E4%BD%9C%E6%B5%81%E7%A8%8B/" rel="prev"><span class="meta-nav">←</span> 简述vue和django的工作流程</a></span>
    
    
        <span class="nav-next"><a href="/tefuir/2021/01/19/git%20%E4%B8%8A%E4%BC%A0/" rel="next">(no title) <span class="meta-nav">→</span></a></span>
    
</nav><!-- .nav-single -->







</div></div>
        <div id="secondary" class="widget-area" role="complementary">
  
    <aside id="search" class="widget widget_search"><form role="search" method="get" accept-charset="utf-8" id="searchform" class="searchform" action="//google.com/search">
    <div>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="搜索" />
    </div>
</form></aside>
  
    
  
    
  
    
  <aside class="widget">
    <h3 class="widget-title">Recents</h3>
    <div class="widget-content">
      <ul>
        
          <li>
            <a href="/tefuir/2021/04/26/python%E5%9F%BA%E7%A1%80%E7%90%86%E8%AE%BA%E5%AE%9D%E5%85%B8/">python基础理论宝典</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/python3-Flask%E7%BB%93%E5%90%88Socket-io%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0%E5%9C%A8%E7%BA%BF%E5%AE%A2%E6%9C%8D%E7%B3%BB%E7%BB%9F/">python3+Flask结合Socket.io简单实现在线客服系统</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%88%A9%E7%94%A8docker%E9%83%A8%E7%BD%B2gunicorn-Flask%E6%89%93%E9%80%A0%E7%8B%AC%E7%AB%8B%E9%95%9C%E5%83%8F/">win10系统下利用docker部署gunicorn+Flask打造独立镜像</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%9F%BA%E4%BA%8Edocker%E9%85%8D%E7%BD%AEelasticsearch%E9%85%8D%E5%90%88python3%E8%BF%9B%E8%A1%8C%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2/">win10系统下基于docker配置elasticsearch配合python3进行全文检索</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%97%AE%E9%A2%98/">服务器问题</a>
          </li>
        
      </ul>
    </div>
  </aside>

  
    
  
    
  
</div>
      </div>
      <footer id="colophon" role="contentinfo">
    <p>&copy; 2021 南辞
    All rights reserved.</p>
    <p>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></p>
</footer>
    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"1","bdMiniList":false,"bdPic":"","bdStyle":"2","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/js/share.js'];</script>

<script src="/js/jquery-3.3.1.min.js"></script>


  
<link rel="stylesheet" href="/tefuir/fancybox/jquery.fancybox.css">

  
<script src="/tefuir/fancybox/jquery.fancybox.pack.js"></script>




<script src="/tefuir/js/script.js"></script>


<script src="/js/navigation.js"></script>

<div id="bg"></div>

  </div>
</body>
</html>